﻿using WRJClient.DAL;
using WRJClient.Net;
using WRJClient.Common;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using WRJClient.UI.Map;
using WRJClient.ViewModel;
using WRJClient.UI;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Threading;
using Esri.ArcGISRuntime.Geometry;
using System.Windows.Input;

namespace WRJClient.UI.OverallState
{
    /// <summary>
    /// ConsoleWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConsoleWindow : Window
    {
        private ConsoleWindowController _controller;
        public MainWindow MainWindow { get; set; }
        public ConsoleWindow()
        {
            InitializeComponent();
            init();
        }

        public void ShowLoginWindow()
        {
            LoginWindow login = new LoginWindow { Owner = this, TitleName = "无人机巡检系统综合态势" };
            bool? result = login.ShowDialog();
            if (result != true)
            {
                //showLoginWindow();
                ShowLoginButton.IsChecked = false;
            }
            else
            {
                ShowLoginButton.IsChecked = true;
            }
            loadDataAsync();
            MapManager.Instance.Init(MainWindow.mapControl);
            ActiveMqConnector.GetInstance().Connect();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Api.Token))
            {
                ShowLoginWindow();

            }

        }

        private async void loadDataAsync()
        {
            await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
             {
                 LoadData();
             });

        }

        private void LoadData()
        {
            DataLoader.Instance.LoadControlCenter();
            Thread.Sleep(100);
            DataLoader.Instance.LoadUAV();
            Thread.Sleep(100);
            DataLoader.Instance.LoadWorkStation();
            Thread.Sleep(100);
            DataLoader.Instance.LoadMission();
            Thread.Sleep(100);
            DataLoader.Instance.LoadZone();
            Thread.Sleep(100);
            DataLoader.Instance.LoadAirline();
            Thread.Sleep(100);
            DataLoader.Instance.LoadInfomation();
        }

        private void init()
        {
            _controller = new ConsoleWindowController(this);
            MainWindow = new MainWindow();
            dataMonitor.SetMapControl(MainWindow.mapControl);
            ActiveMqConnector.GetInstance().ClientId = "OverallState_" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.fff");


            ControlCenterManager.Instance.OnDataAdded += Instance_OnDataAdded;
            UAVManager.Instance.OnDataAdded += Instance_OnDataAdded;
            WorkStationManager.Instance.OnDataAdded += Instance_OnDataAdded;
            InfomationManager.Instance.OnDataAdded += Instance_OnDataAdded;
            InfomationManager.Instance.OnDataRemoved += Instance_OnDataRemoved;
            ZoneManager.Instance.OnDataAdded += Instance_OnDataAdded;
            AirlineManager.Instance.OnDataAdded += Instance_OnDataAdded;

            toolbarPanel.DataContext = MapManager.Instance;

        }

        private void Instance_OnDataRemoved(object sender, DataChangedEventArgs e)
        {
            IMapObject mapObject = e.Data as IMapObject;
            mapObject?.RemoveGraphics();
        }

        private void Instance_OnDataAdded(object sender, DataChangedEventArgs e)
        {
            IMapObject mapObject = e.Data as IMapObject;
            mapObject?.AddGraphics();
            AirlineViewModel airline = e.Data as AirlineViewModel;
        }

        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton toggleButton = sender as ToggleButton;
            if (toggleButton != null)
            {
                _controller.CommandItemSelect(toggleButton.Name, toggleButton.IsChecked);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                _controller.CommandItemSelect(button.Name);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.KeyboardDevice.Modifiers == ModifierKeys.Control)
            {
                switch (e.Key)
                {
                    case Key.D0:
                        MapManager.Instance.SetViewPoint(0);
                        break;
                    case Key.D1:
                        MapManager.Instance.SetViewPoint(0);
                        break;
                    case Key.D2:
                        MapManager.Instance.SetViewPoint(1);
                        break;
                    case Key.D3:
                        MapManager.Instance.SetViewPoint(2);
                        break;
                    case Key.D4:
                        MapManager.Instance.SetViewPoint(3);
                        break;
                    case Key.D5:
                        MapManager.Instance.SetViewPoint(4);
                        break;
                    case Key.D6:
                        MapManager.Instance.SetViewPoint(5);
                        break;
                    case Key.D7:
                        MapManager.Instance.SetViewPoint(6);
                        break;
                    case Key.D8:
                        MapManager.Instance.SetViewPoint(7);
                        break;
                    case Key.D9:
                        MapManager.Instance.SetViewPoint(8);
                        break;
                    case Key.OemPlus:
                    case Key.Add:
                        MapManager.Instance.ZoomOut(1000);
                        break;
                    case Key.PageUp:
                        MapManager.Instance.ZoomOut(5000);
                        break;
                    case Key.OemMinus:
                    case Key.Subtract:
                        MapManager.Instance.ZoomIn(1000);
                        break;
                    case Key.PageDown:
                        MapManager.Instance.ZoomIn(5000);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if (Keyboard.IsKeyDown(Key.M))
                {
                    if (Keyboard.IsKeyDown(Key.D1))
                    {
                        MapManager.Instance.ChangeMap(0);
                    }
                    else if (Keyboard.IsKeyDown(Key.D2))
                    {
                        MapManager.Instance.ChangeMap(1);
                    }
                }
            }
        }
    }
}
